using HarmonyLib;
using RimWorld;
using System.Reflection;
using Verse;
using System.Reflection.Emit;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System;
using Verse.AI;
using RimWorld.Planet;
using RimZombieGirl;

namespace RimZombieGirl
{
    public class RimZombieGirlMod : Mod
    {

        public RimZombieGirlMod(ModContentPack content) : base(content)
        {

            #if DEBUG
            Log.Message("RimZombieGirlMod: loaded");
            #endif
            var harmony = new Harmony("RI.RimImmortal.Homunculus");

        }
    }

        public class RimZombieGirlModComponent : GameComponent
    {
        public RimZombieGirlModComponent(Game game)
        {
        }
        public override void ExposeData()
        {
            if (Scribe.mode == LoadSaveMode.PostLoadInit)
            {
                // 清理缓存
                DeadNotDeadCache.Clear();
                // PawnControl.Clear();
            }
        }


    }

    [StaticConstructorOnStartup]
    public static class Main
    {
        static readonly bool HARactive = (LoadedModManager.RunningModsListForReading
        .Any<ModContentPack>((Predicate<ModContentPack>)(x => x.Name.Contains("Humanoid Alien Races")))
        || LoadedModManager.RunningModsListForReading
        .Any<ModContentPack>((Predicate<ModContentPack>)(x => x.PackageId == "erdelf.HumanoidAlienRaces"))
        );
        static Main()
        {
            Log.Message("FacelessManMod: StaticConstructorOnStartup");
            // var harmony = new Harmony("nightz.facelessman");
            // harmony.PatchAll(Assembly.GetExecutingAssembly());
            // #if DEBUG
            // Log.Message("FacelessManMod loaded");
            // #endif
            Harmony harmony = new Harmony("ZombieGirl");
            harmony.PatchAll(Assembly.GetExecutingAssembly());
            try
            {
                ((Action)(() =>
                {
                    harmony.Patch((MethodBase)AccessTools.Method(typeof(PawnGenerator), "GetBodyTypeFor"), postfix: new HarmonyMethod(typeof(ZGPatches.ZGGetBodyTypeFor), "Postfix"));
                    harmony.Patch((MethodBase)AccessTools.Method(typeof(Pawn_GeneTracker), "Notify_GenesChanged"), postfix: new HarmonyMethod(typeof(ZGPatches.ZGNotify_GenesChanged), "Postfix"));
                    harmony.Patch((MethodBase)AccessTools.Method(typeof(LifeStageWorker_HumanlikeAdult), "Notify_LifeStageStarted"), prefix: new HarmonyMethod(typeof(ZGPatches.ZGNotify_LifeStageStarted), "Prefix"));
                    harmony.Patch((MethodBase)AccessTools.Method(typeof(ApparelRequirement), "AllowedForPawn"), postfix: new HarmonyMethod(typeof(ZGPatches.ZGAllowedForPawn), "Postfix"));
                    harmony.Patch((MethodBase)AccessTools.Method(typeof(JobGiver_OptimizeApparel), "ApparelScoreGain"), postfix: new HarmonyMethod(typeof(ZGPatches.ZGApparelScoreGain), "Postfix"));
                    harmony.Patch((MethodBase)AccessTools.Method(typeof(PawnApparelGenerator), "CanUsePair"), postfix: new HarmonyMethod(typeof(ZGPatches.ZGCanUsePair), "Postfix"));
                    harmony.Patch((MethodBase)AccessTools.Method(typeof(ApparelGraphicRecordGetter), "TryGetGraphicApparel"), prefix: new HarmonyMethod(typeof(ZGPatches.ZGResolveApparelGraphic), "Prefix"));
                    harmony.Patch(typeof(EquipmentUtility).GetMethod("CanEquip", new[] { typeof(Thing), typeof(Pawn), typeof(string).MakeByRefType(), typeof(bool) }), postfix: new HarmonyMethod(typeof(ZGPatches.ZGCanEquip), "Postfix"));
                    harmony.Patch((MethodBase)AccessTools.Method(typeof(HeadTypeDef), "GetGraphic"), prefix: new HarmonyMethod(typeof(ZGPatches.ZGHeadDefGetGraphic), "Prefix"));

                    harmony.Patch((MethodBase)AccessTools.Method(typeof(PawnRenderNode_Body), "GraphicFor"), postfix: new HarmonyMethod(typeof(ZGPatches.ZGBodyNodeGraphicFor), "Postfix"));
                    harmony.Patch((MethodBase)AccessTools.Method(typeof(Pawn_StoryTracker), "ExposeData"), transpiler: new HarmonyMethod(typeof(ZGPatches.ZGDisableLoadChildBodyFailsafe), "Transpiler"));


                    if (HARactive)
                    {
                        Log.Message("[ZombieGirl] Detected HAR!");
                        HARPatcher.patch();
                    }
                    Log.Message("[ZombieGirl] Active.");
                }))();
            }
            catch (TypeLoadException e)
            {
                Log.Error("[ZombieGirl] TypeLoadException:");
                Log.Error(e.StackTrace);
            }
        }
    }

}